package com.changpei.core.utils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.changpei.common.utils.BeanUtil;

import java.util.Map;
import java.util.Set;

/**
 *
 *
 * @author Way on 2021/8/12
 */
public class SearchUtil {

    /**
     * 初始化查询
     *
     * @param obj
     * @param <T>
     * @return
     */
    public static <T> QueryWrapper<T> initQuery(T obj) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        Map<String, Object> param = BeanUtil.objectToMap(obj);
        if (!param.isEmpty()) {
            Set<Map.Entry<String, Object>> entries = param.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String key = entry.getKey();
                Object value = entry.getValue();
                // 添加查询参数
                queryWrapper.eq(BeanUtil.humpToLine(key), value);
            }
        }
        return queryWrapper;
    }
}
